package com.zsd.android.ffmpegdemo;

import android.Manifest;
import android.os.Environment;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.SeekBar;

import com.zsd.android.ffmpegdemo.player.FFMPegPlayer;
import com.zsd.android.ffmpegdemo.utils.MLog;
import com.zsd.android.ffmpegdemo.utils.PermissionHelper;

import java.io.File;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, FFMPegPlayer.OnPrepareListener, FFMPegPlayer.OnErrorListener, FFMPegPlayer.OnProgressListener, SeekBar.OnSeekBarChangeListener {

    private SurfaceView mSurfaceView;
    private SeekBar seekBar;

    private FFMPegPlayer mPlayer;
    private boolean isStarting;
    private int progress;
    private boolean isTouch, isSeek;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_main);
        mSurfaceView = findViewById(R.id.main_surface);
        seekBar = findViewById(R.id.main_seekBar);
        findViewById(R.id.play_btn).setOnClickListener(this);
        findViewById(R.id.stop_btn).setOnClickListener(this);
        mPlayer = new FFMPegPlayer();
        mPlayer.setSurfaceView(mSurfaceView);
        mPlayer.setOnPrepareListener(this);
        mPlayer.setOnErrorListener(this);
        mPlayer.setOnProgressListener(this);
        PermissionHelper.getInstance(this).isPermissions(this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 200);
        seekBar.setOnSeekBarChangeListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.play_btn:
                if (isStarting) {
                    return;
                }
                isStarting = true;
                File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "DCIM/Camera/VID_20190306_160632.mp4");
                MLog.i("videoPath = " + file.getAbsolutePath());
                mPlayer.prepare(file.getAbsolutePath());
//                mPlayer.prepare("https://media.w3.org/2010/05/sintel/trailer.mp4");
                break;
            case R.id.stop_btn:
                if (isStarting) {
                    isStarting = false;
                    mPlayer.stop();
                }
                break;
        }
    }

    @Override
    public void onPrepare() {
        MLog.i("onPrepare , start()");
        int duration = mPlayer.getDuration();
        MLog.i("duration = " + duration);
        if (duration > 0) {
            runOnUiThread(() -> {
                seekBar.setVisibility(View.VISIBLE);
            });
        }
        mPlayer.start();
    }

    @Override
    public void onError(int error) {
        MLog.i("onError " + error);
    }

    @Override
    public void onProgress(int progress) {
        if (!isTouch) {
            runOnUiThread(() -> {
                int duration = mPlayer.getDuration();
                if (duration > 0) {
                    if (isSeek) {
                        isSeek = false;
                        return;
                    }
                    seekBar.setProgress(progress * 100 / duration);
                }
            });
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        isTouch = true;
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        isTouch = false;
        isSeek = true;
        progress = mPlayer.getDuration() * seekBar.getProgress() / 100;
        mPlayer.seek(progress);
    }
}
